<!doctype html>
<html lang="zh-Hans">
	<head>
		<title>电阻分压计算器 - 嵌入式工具集</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<!-- 以下为浏览器中的配置，正式 编译之后请注释掉这段-->
		<link rel="stylesheet" href="../styles/styles.css" />
		<!-- 以下为LCEDA中的配置，正式 编译之后请勿注释这段-->
		<link rel="stylesheet" href="/iframe/styles/styles.css" />

		<style>
			/* 电阻分压计算器专用样式 */
			.calculator-container {
				background: white;
				border-radius: 12px;
				box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
			}

			.input-group {
				display: flex;
				align-items: stretch;
				gap: 0;
				margin-bottom: 0.75rem;
				background-color: #f8fafc;
				border-radius: 8px;
				overflow: hidden;
				transition: all 0.2s ease;
			}

			.input-group:hover {
				background-color: #f1f5f9;
				box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
			}

			.input-label {
				background-color: #4a90e2;
				color: white;
				padding: 0.75rem 1rem;
				font-weight: 600;
				min-width: 120px;
				text-align: center;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-shrink: 0;
			}

			.input-field {
				flex: 1;
				position: relative;
				display: flex;
				align-items: center;
			}

			.input-value {
				width: calc(100% - 3rem);
				padding: 0.75rem 1rem;
				border: none;
				background: transparent;
				font-size: 1rem;
				outline: none;
				color: #374151;
			}

			.input-value:focus {
				background-color: white;
			}

			.unit-selector {
				width: 3rem;
				text-align: center;
				background: transparent;
				border: none;
				border-left: 1px solid #e2e8f0;
				font-size: 0.875rem;
				color: #6b7280;
				cursor: pointer;
				padding: 0.75rem 0.25rem;
				transition: all 0.2s ease;
			}

			.unit-selector:hover {
				background-color: white;
				border-color: #cbd5e1;
				color: #475569;
			}

			.formula-display {
				background-color: #f8fafc;
				border-radius: 8px;
				padding: 1rem;
				margin-bottom: 1.5rem;
				font-family: monospace;
				text-align: center;
				border: 1px solid #e2e8f0;
			}

			.formula-title {
				font-weight: bold;
				margin-bottom: 0.5rem;
				color: #475569;
			}

			.formula-text {
				color: #334155;
				font-size: 0.875rem;
			}

			.btn-calculate {
				background-color: #10b981;
				color: white;
				border: none;
				border-radius: 6px;
				padding: 0.75rem 1rem;
				cursor: pointer;
				transition: all 0.2s ease;
				font-weight: 500;
				display: block;
				width: 100%;
				margin-bottom: 1rem;
				font-size: 1rem;
			}

			.btn-calculate:hover {
				background-color: #059669;
				transform: translateY(-1px);
			}

			.btn-reset {
				background-color: #f5f5f5;
				color: #555;
				border: none;
				border-radius: 6px;
				padding: 0.75rem 1rem;
				cursor: pointer;
				transition: all 0.2s ease;
				font-weight: 500;
				display: block;
				width: 100%;
				font-size: 1rem;
			}

			.btn-reset:hover {
				background-color: #e0e0e0;
				transform: translateY(-1px);
			}

			.result-card {
				background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
				color: white;
				border-radius: 12px;
				padding: 1.5rem;
				text-align: center;
				margin-bottom: 1.5rem;
				transition: transform 0.2s ease;
			}

			.result-card:hover {
				transform: translateY(-2px);
				box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
			}

			.result-title {
				font-size: 1.2rem;
				font-weight: 600;
				margin-bottom: 0.5rem;
			}

			.result-value {
				font-size: 2rem;
				font-weight: 700;
				margin: 0.5rem 0;
			}

			/* 响应式设计 */
			@media (max-width: 640px) {
				.input-label {
					min-width: 100px;
					font-size: 0.875rem;
					padding: 0.75rem 0.5rem;
				}

				.result-value {
					font-size: 1.5rem;
				}
			}
		</style>
	</head>
	<body class="bg-gradient-to-br from-light to-blue-50 min-h-screen p-4 md:p-6 font-sans">
		<div class="max-w-md mx-auto bg-white rounded-2xl shadow-lg p-6 md:p-8">
			<h2 class="text-2xl md:text-3xl font-bold text-primary mb-6 text-center">电阻分压计算器</h2>

			<div class="formula-display">
				<div class="formula-title">计算公式</div>
				<div class="formula-text">V<sub>out</sub> = V<sub>in</sub> × R<sub>2</sub> / (R<sub>1</sub> + R<sub>2</sub>)</div>
			</div>

			<div class="input-group">
				<div class="input-label">V<sub>in</sub></div>
				<div class="input-field">
					<input type="number" id="vin" class="input-value" value="5" min="0" step="0.1" placeholder="输入电压" />
					<select class="unit-selector" id="vin-unit">
						<option value="volt" selected>V</option>
					</select>
				</div>
			</div>

			<div class="input-group">
				<div class="input-label">R<sub>1</sub></div>
				<div class="input-field">
					<input type="number" id="r1" class="input-value" value="10000" min="0.1" step="0.1" placeholder="上分压电阻" />
					<select class="unit-selector" id="r1-unit">
						<option value="ohm">Ω</option>
						<option value="kilo-ohm" selected>kΩ</option>
						<option value="mega-ohm">MΩ</option>
					</select>
				</div>
			</div>

			<div class="input-group">
				<div class="input-label">R<sub>2</sub></div>
				<div class="input-field">
					<input type="number" id="r2" class="input-value" value="10000" min="0.1" step="0.1" placeholder="下分压电阻" />
					<select class="unit-selector" id="r2-unit">
						<option value="ohm">Ω</option>
						<option value="kilo-ohm" selected>kΩ</option>
						<option value="mega-ohm">MΩ</option>
					</select>
				</div>
			</div>

			<button id="calc-btn" class="btn-calculate">计算</button>
			<button id="reset-btn" class="btn-reset">重置</button>

			<div class="result-card">
				<div class="result-title">输出电压 (V<sub>out</sub>)</div>
				<div class="result-value" id="vout">2.50 V</div>
			</div>
		</div>

		<script>
			// 单位转换函数
			function convertToBaseUnit(value, unit) {
				const numValue = parseFloat(value) || 0;
				switch (unit) {
					case 'kilo-ohm':
						return numValue * 1000;
					case 'mega-ohm':
						return numValue * 1000000;
					case 'volt':
					case 'ohm':
					default:
						return numValue;
				}
			}

			// 格式化结果显示
			function formatResult(value, unitType) {
				if (unitType === 'voltage') {
					return value.toFixed(2) + ' V';
				} else if (unitType === 'resistance') {
					if (value >= 1000000) {
						return (value / 1000000).toFixed(2) + ' MΩ';
					} else if (value >= 1000) {
						return (value / 1000).toFixed(2) + ' kΩ';
					} else {
						return value.toFixed(2) + ' Ω';
					}
				}
				return value.toString();
			}

			// 计算函数
			function calculate() {
				const vin = parseFloat(document.getElementById('vin').value) || 0;
				const r1 = parseFloat(document.getElementById('r1').value) || 0;
				const r2 = parseFloat(document.getElementById('r2').value) || 0;

				const r1Unit = document.getElementById('r1-unit').value;
				const r2Unit = document.getElementById('r2-unit').value;

				// 转换为基本单位
				const r1Base = convertToBaseUnit(r1, r1Unit);
				const r2Base = convertToBaseUnit(r2, r2Unit);

				if (vin >= 0 && r1Base > 0 && r2Base > 0) {
					const vout = (vin * r2Base) / (r1Base + r2Base);

					// 添加结果更新的动画效果
					const resultElement = document.getElementById('vout');
					resultElement.style.opacity = '0.5';
					setTimeout(() => {
						resultElement.textContent = formatResult(vout, 'voltage');
						resultElement.style.opacity = '1';
					}, 150);
				}
			}

			// 重置函数
			function resetCalculator() {
				document.getElementById('vin').value = 5;
				document.getElementById('r1').value = 10000;
				document.getElementById('r2').value = 10000;
				document.getElementById('r1-unit').value = 'kilo-ohm';
				document.getElementById('r2-unit').value = 'kilo-ohm';
				document.getElementById('vout').textContent = '2.50 V';
			}

			// 绑定事件
			document.getElementById('calc-btn').addEventListener('click', calculate);
			document.getElementById('reset-btn').addEventListener('click', resetCalculator);

			// 为输入和选择器添加实时计算事件
			const inputs = document.querySelectorAll('input, select');
			inputs.forEach((input) => {
				input.addEventListener('change', calculate);
			});

			// 页面加载时初始化
			window.onload = function () {
				calculate();
			};
		</script>

		<!-- 页脚 -->
		<footer class="max-w-md mx-auto mt-8 text-center text-sm text-gray-500">
			<p>版本号: V0.0.1</p>
		</footer>
	</body>
</html>
